import time
import pandas as pd
from bs4 import BeautifulSoup
import requests as sys_requests


def serialization_data(code, year, quarter):
    def get_from_sina_api(code, year, quarter):
        url = "http://money.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/%s.phtml?year=%s&jidu=%s" % (
            code, year, quarter)
        response = sys_requests.get(url)
        content = str(response.content, 'gbk')
        return content

    content = get_from_sina_api(code, year, quarter)
    soup = BeautifulSoup(content, "html.parser")
    shares_table = soup.find(id="FundHoldSharesTable")
    if shares_table != None:
        stock_name = shares_table.find("th").get_text().strip().split("年")[0]
        list_table = shares_table.find_all("tr")
        list_table.pop(0)
        page_title = list_table.pop(0)
        columns = ["date", "open", "high", "close", "low", "volume", "volume_num"]
        list_date = []
        list_open_value = []
        list_close_value = []
        list_low = []
        list_high = []
        list_volume_num = []
        list_volume = []

        for line in list_table:
            td = line.find_all("td")
            try:
                date = str(td[0].find("a").get_text()).strip()
            except Exception as e:
                date = str(td[0].find("div").get_text()).strip()
            list_date.append(date)

            open_value = float(str(td[1].find("div").get_text()).strip())
            list_open_value.append(open_value)

            high = float(str(td[2].find("div").get_text()).strip())
            list_high.append(high)

            close_value = float(str(td[3].find("div").get_text()).strip())
            list_close_value.append(close_value)

            low = float(str(td[4].find("div").get_text()).strip())
            list_low.append(low)

            volume_num = int(str(td[5].find("div").get_text()).strip())
            list_volume_num.append(volume_num)

            volume = int(str(td[6].find("div").get_text()).strip())
            list_volume.append(volume)

        dict_table = {"date": list_date, "open": list_open_value,
                      'close': list_close_value,
                      'high': list_high, 'low': list_low,
                      "volume": list_volume,
                      "volume_num": list_volume_num
                      }
        data = pd.DataFrame(dict_table, columns=columns)
        data.date = pd.to_datetime(data.date)

        return stock_name, data.sort_values(by="date")

    else:
        raise Exception("该股票或日期不存在", "年份:%s" % year, "代码:%s" % code, "季度:%s" % quarter)


def load_train_data(code, year):
    now_quarter = int(time.strftime('%m', time.localtime(time.time()))) // 4 + 1
    now_year = int(time.strftime('%Y', time.localtime(time.time())))
    max_quarter = 4
    if now_year < int(year):
        raise Exception("时间大于当前日期")
    elif now_year == int(year):
        max_quarter = now_quarter
    stock_name, resources = serialization_data(code, year, 1)
    for quarter in range(2, max_quarter + 1):
        t_name, t_resources = serialization_data(code, year, quarter)
        resources = pd.concat([resources, t_resources], axis=0, ignore_index=True)
    resources.sort_values(by="date")
    return stock_name, resources


def load_train_data_by_year(code, start_year, end_year):
    start_year=int(start_year)
    end_year=int(end_year)
    stock_name, data = load_train_data(code, start_year)
    if end_year < start_year < 0:
        raise Exception("起止日期错误")
    elif end_year == start_year:
        return stock_name,data
    for year in range(start_year + 1, end_year + 1):
        t_name, t_data = load_train_data(code, year)
        data = pd.concat([data, t_data], axis=0, ignore_index=True)
    data.sort_values(by="date")
    return stock_name, data
